package com.lt.tccdemo.config;

import com.mysql.cj.jdbc.MysqlDataSource;
import com.mysql.cj.jdbc.MysqlXADataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.io.IOException;

/**
 * @author lucine
 * @version 1.0 2021/9/27 11:57
 */
@Configuration
@MapperScan(value = "com.lt.tccdemo.db109.dao",sqlSessionFactoryRef = "sqlSessionFactoryBean109")
public class ConfigDb109 {


    @Bean("db109")
    public DataSource db109(){
        MysqlDataSource dataSource =new MysqlXADataSource();
        dataSource.setURL("jdbc:mysql://192.168.1.109:3306/xa109");
        dataSource.setUser("lt");
        dataSource.setPassword("P@ssw0rd");
        return dataSource;
    }

    @Bean("sqlSessionFactoryBean109")
    public SqlSessionFactoryBean sqlSessionFactoryBean(@Qualifier("db109") DataSource dataSource) throws IOException {
        SqlSessionFactoryBean sqlSessionFactoryBean =new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
        sqlSessionFactoryBean.setMapperLocations(resourceResolver.getResources("mybatis/db109/*.xml"));
        return sqlSessionFactoryBean;
    }
    @Bean("xm109")
    public PlatformTransactionManager platformTransactionManager(@Qualifier("db109") DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }

}
